In [1]:
import pandas as pd
import plotly.express as px
import numpy as np
%config IPCompleter.greedy=True

from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

3. Load election_context-2018.csv and use Plotly to create the following maps. Each map must have a title and legend.

In [2]:
election = pd.read_csv('election-context-2018.csv')
election.head(2)
Out[2]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age29andunder_pct age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc
0 Alabama Autauga 1001 18172 5936 865 17379 6363 190 6331.0 ... 40.037058 13.978456 53099.0 5.591657 12.417046 75.407229 10.002112 74.065601 42.002162 2.0
1 Alabama Baldwin 1003 72883 18458 3874 66016 18424 898 19145.0 ... 35.474412 18.714851 51365.0 6.286843 9.972418 70.452889 7.842227 68.405607 42.279099 3.0

2 rows × 39 columns

a. (Figure 1) A Choropleth map showing the 2016 county by county senate election results in the state of Georgia. The map should show the map of Georgia and only Georgia.

i. For each county, compare the columns “demsen16”, “repsen16”, and “othersen16”. If “demsen16” has the highest number, color the county blue in the map. If “repsen16” has the highest number, color the county red in the map. If “othersen16” has the highest number, color the county white in the map. ii. The border of each county should be black.

In [3]:
Georgia = election['state'].isin(['Georgia'])
In [4]:
Georgia = election[Georgia]
In [5]:
Georgia.head(2)
Out[5]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age29andunder_pct age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc
358 Georgia Appling 13001 5494 1434 84 5233 1758 95 1187.0 ... 38.978816 15.524172 37388.0 8.460929 23.624858 88.082103 19.210959 86.158904 71.441106 7.0
359 Georgia Atkinson 13003 1878 697 35 1938 930 34 596.0 ... 43.226899 11.465893 31296.0 5.914870 32.105162 93.321819 22.363919 90.451011 100.000000 9.0

2 rows × 39 columns

In [6]:
Georgia['winner'] = np.where(Georgia['demsen16']>Georgia['repsen16'], 
                                           'demsen16', 'repsen16')
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [7]:
Georgia.head(2)
Out[7]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc winner
358 Georgia Appling 13001 5494 1434 84 5233 1758 95 1187.0 ... 15.524172 37388.0 8.460929 23.624858 88.082103 19.210959 86.158904 71.441106 7.0 repsen16
359 Georgia Atkinson 13003 1878 697 35 1938 930 34 596.0 ... 11.465893 31296.0 5.914870 32.105162 93.321819 22.363919 90.451011 100.000000 9.0 repsen16

2 rows × 40 columns

In [8]:
demsen16 = Georgia['winner'].isin(['demsen16'])
demsen16 = Georgia[demsen16]
demsen16['winner'] = np.where(demsen16['demsen16']>demsen16['othersen16'], 'demsen16','othersen16')
demsen16
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Out[8]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc winner
368 Georgia Bibb 13021 24043 36787 1766 25623 38585 510 31533.0 ... 13.935043 37108.0 11.320146 15.711283 75.410133 10.139883 65.621051 14.410435 3.0 demsen16
376 Georgia Calhoun 13037 830 1179 16 883 1298 7 1007.0 ... 12.525221 28200.0 14.409811 24.015997 89.707430 19.019492 83.756645 100.000000 8.0 demsen16
382 Georgia Chatham 13051 45688 62290 4164 47204 60246 1178 52146.0 ... 13.588547 49596.0 9.248417 10.708225 67.105235 6.695639 58.574929 4.504994 2.0 demsen16
386 Georgia Clarke 13059 12717 29603 2656 13815 25431 1122 25474.0 ... 9.597089 33116.0 8.619281 13.787120 59.246741 4.818434 43.629707 5.859623 3.0 demsen16
387 Georgia Clay 13061 566 697 10 537 862 3 574.0 ... 23.891786 22120.0 18.940137 17.658349 92.562380 9.626955 86.040915 100.000000 9.0 demsen16
388 Georgia Clayton 13063 12645 78220 1994 14164 81479 424 69400.0 ... 8.178148 42470.0 12.178854 16.439458 80.971266 19.143296 85.286208 0.887736 1.0 demsen16
401 Georgia DeKalb 13089 51468 251370 11919 64392 238224 3649 218383.0 ... 10.332062 52623.0 9.764153 11.433755 58.255458 3.233796 32.371757 0.263046 1.0 demsen16
404 Georgia Dougherty 13095 10232 23311 544 11449 26295 175 20354.0 ... 13.478327 33605.0 17.025370 17.839948 80.234198 11.899813 71.750600 13.959710 3.0 demsen16
405 Georgia Douglas 13097 24817 31005 1916 26241 28441 585 28166.0 ... 10.349790 57384.0 9.039715 11.743555 73.852329 13.699280 79.869939 15.757196 1.0 demsen16
417 Georgia Fulton 13121 117783 297051 16557 137124 255470 4819 246397.0 ... 10.393807 58851.0 8.900157 8.677323 50.193906 2.481009 31.443299 1.077689 1.0 demsen16
427 Georgia Hancock 13141 843 2701 36 769 3308 11 2295.0 ... 19.389505 26386.0 10.310510 27.094501 89.110124 12.021277 81.382979 61.586595 7.0 demsen16
438 Georgia Jefferson 13163 3063 3821 84 2999 4261 38 3091.0 ... 16.881674 28417.0 13.283534 25.535681 89.584498 14.337789 83.333333 80.667454 6.0 demsen16
446 Georgia Liberty 13179 6134 9556 453 5565 10457 148 8147.0 ... 7.485653 42484.0 12.997548 10.343577 81.100452 9.838586 81.725596 23.160449 3.0 demsen16
453 Georgia Macon 13193 1540 2705 42 1545 3211 25 2129.0 ... 14.394049 28285.0 16.925113 27.824180 91.470035 23.548293 87.919463 53.188602 6.0 demsen16
463 Georgia Muscogee 13215 26976 39851 2205 27510 42573 520 33620.0 ... 12.000320 42661.0 10.053196 13.388110 74.998051 9.089200 67.241969 2.977592 2.0 demsen16
478 Georgia Richmond 13245 24461 48814 2240 25845 52560 615 42229.0 ... 12.556971 38595.0 11.484693 16.424238 78.956132 13.729355 72.185329 9.216202 2.0 demsen16
479 Georgia Rockdale 13247 13478 23255 1136 15716 22023 346 21223.0 ... 12.570659 51072.0 10.262957 12.875801 74.038935 11.013381 77.131185 14.932817 1.0 demsen16
485 Georgia Stewart 13259 805 1222 31 745 1323 8 900.0 ... 15.230530 20882.0 13.892529 37.892483 89.581351 19.005613 78.508420 100.000000 8.0 demsen16
487 Georgia Talbot 13263 1196 2002 48 1202 2265 24 1639.0 ... 19.613997 36957.0 9.312871 21.436227 87.352626 14.871542 80.583004 93.882010 8.0 demsen16
488 Georgia Taliaferro 13265 349 545 3 323 636 4 443.0 ... 22.321429 28152.0 11.650485 32.776618 91.231733 13.763441 82.795699 100.000000 8.0 demsen16
506 Georgia Warren 13301 991 1314 29 990 1529 7 1110.0 ... 20.476017 31875.0 12.541806 26.283368 87.910678 14.145141 84.194342 100.000000 8.0 demsen16

21 rows × 40 columns

In [9]:
repsen16 = Georgia['winner'].isin(['repsen16'])
repsen16 = Georgia[repsen16]
repsen16['winner'] = np.where(repsen16['repsen16']>repsen16['othersen16'], 'repsen16','othersen16')
repsen16
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

Out[9]:
state county fips trump16 clinton16 otherpres16 romney12 obama12 otherpres12 demsen16 ... age65andolder_pct median_hh_inc clf_unemploy_pct lesshs_pct lesscollege_pct lesshs_whites_pct lesscollege_whites_pct rural_pct ruralurban_cc winner
358 Georgia Appling 13001 5494 1434 84 5233 1758 95 1187.0 ... 15.524172 37388.0 8.460929 23.624858 88.082103 19.210959 86.158904 71.441106 7.0 repsen16
359 Georgia Atkinson 13003 1878 697 35 1938 930 34 596.0 ... 11.465893 31296.0 5.914870 32.105162 93.321819 22.363919 90.451011 100.000000 9.0 repsen16
360 Georgia Bacon 13005 3364 608 48 3093 791 47 474.0 ... 14.345392 37303.0 5.287009 19.885120 86.802516 16.443529 88.070717 69.286229 7.0 repsen16
361 Georgia Baker 13007 775 650 15 785 794 10 531.0 ... 19.538462 45903.0 3.202847 21.246340 89.000418 14.389652 88.197251 100.000000 3.0 repsen16
362 Georgia Baldwin 13009 7697 7970 449 7589 8483 166 6787.0 ... 13.783619 34595.0 8.202602 18.346188 81.578475 12.792543 73.112521 35.139983 4.0 repsen16
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
512 Georgia Whitfield 13313 21537 7937 1184 19305 7210 361 6763.0 ... 12.708749 41764.0 9.632391 31.089118 86.449311 20.838944 83.476973 29.077281 3.0 repsen16
513 Georgia Wilcox 13315 2096 852 28 2053 1060 16 669.0 ... 16.062584 34071.0 7.889908 22.582167 90.470200 15.937500 87.395833 100.000000 8.0 repsen16
514 Georgia Wilkes 13317 2572 1848 66 2635 2087 39 1525.0 ... 21.372430 33462.0 8.615538 22.051133 86.214410 16.580844 79.042225 67.365241 6.0 repsen16
515 Georgia Wilkinson 13319 2333 1894 60 2246 2181 23 1616.0 ... 17.772766 38827.0 7.379973 17.328351 91.359178 13.307292 89.921875 100.000000 8.0 repsen16
516 Georgia Worth 13321 6152 2020 123 5869 2487 63 1839.0 ... 16.900133 38684.0 8.232804 22.560336 89.786639 17.752765 90.748420 69.163707 3.0 repsen16

138 rows × 40 columns

In [10]:
Georgia = demsen16.merge(repsen16, how='outer')
In [11]:
fips = Georgia['fips'].tolist()
gElec = px.choropleth(Georgia, geojson = counties, locations = fips, scope = 'usa', color = 'winner', title = 'SenateWinners')
gElec.update_geos(fitbounds="locations", visible=False)
gElec.show()

(Figure 2) Create a similar Choropleth map showing the 2016 county by county house election results in the state of Georgia, using the same color scheme as specified above.

In [12]:
Georgia['houseWinner'] = np.where(Georgia['demhouse16']>Georgia['rephouse16'], 
                                           'demhouse16', 'rephouse16')
In [13]:
demhouse16 = Georgia['houseWinner'].isin(['demhouse16'])
demhouse16 = Georgia[demhouse16]
demhouse16['winner'] = np.where(demhouse16['demhouse16']>demhouse16['otherhouse16'], 'demhouse16','otherhouse16')
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [14]:
rephouse16 = Georgia['houseWinner'].isin(['rephouse16'])
rephouse16 = Georgia[rephouse16]
rephouse16['winner'] = np.where(rephouse16['rephouse16']>rephouse16['otherhouse16'], 'rephouse16','otherhouse16')
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [15]:
Georgia = demhouse16.merge(rephouse16, how='outer')
fips = Georgia['fips'].tolist()
gElecHouse = px.choropleth(Georgia, geojson = counties, locations = fips, scope = 'usa', color = 'winner', title = 'HouseWinners')
gElecHouse.update_geos(fitbounds="locations", visible=False)
gElecHouse.show()

4. Load 1976-2018-senate.csv and use Plotly to create the following map. Every figure must have a title and a legend.

In [16]:
senate = pd.read_csv('1976-2018-senate.csv', encoding= 'unicode_escape')
In [17]:
senate.head(2)
Out[17]:
year state state_po state_fips state_cen state_ic office district stage special candidate party writein mode candidatevotes totalvotes unofficial version
0 1976 Arizona AZ 4 86 61 US Senate statewide gen False Sam Steiger republican False total 321236 741210 False 20171011.0
1 1976 Arizona AZ 4 86 61 US Senate statewide gen False Wm. Mathews Feighan independent False total 1565 741210 False 20171011.0

a. (Figure 3) Create a Choropleth map for the 2018 US senate election. The map should show the entire United States. Each state should be color coded. The value for each state is the winning candidate’s vote percentage. Divide the vote percentage into 6 bins and create a color scale for it.

When the mouse cursor hovers over each state, the winning candidate’s name and party affiliation should be displayed in the tooltip window. For example, Wyoming’s 2018 winning candidate was John Barrasso, Republican. His vote percentage was 136210/203420.

In [18]:
senate['votePer']= senate['candidatevotes']/senate['totalvotes']
In [19]:
fips = senate['state_fips'].tolist()
gSen = px.choropleth(senate, locations = 'state_po', locationmode = 'USA-states', title = 'senate', scope = 'usa', color = 'votePer', color_continuous_scale = px.colors.sequential.Plasma, hover_data = ['candidate', 'party', 'votePer'])
gSen.show()

5. Load 1962_2006_walmart_store_openings.csv and use Plotly to create the following map. The map must have a title and legend.

In [20]:
walmart = pd.read_csv('1962_2006_walmart_store_openings.csv')
walmart.tail(2)
Out[20]:
storenum OPENDATE date_super conversion st county STREETADDR STRCITY STRSTATE ZIPCODE type_store LAT LON MONTH DAY YEAR
2990 3425 1/27/06 1/27/06 0.0 48 201 9598 Rowlett Rd Houston TX 77034 Supercenter 29.636430 -95.21789 1 27 2006
2991 5193 1/31/06 NaN NaN 6 65 12721 Moreno Beach Dr Moreno Valley CA 92555 Wal-Mart 33.922823 -117.16837 1 31 2006

a. (Figure 4) Create a Scattergeo map that shows the location of every Walmart store opened since 2000 (including 2000) in the United States.

b. The map should show the entire United States. c. If it’s a “Supercenter”, use a dark blue color to fill the marker. If it’s a “Wal-Mart”, use a light blue color.

In [21]:
walmart2000 = walmart['YEAR']<=2000
walmart[walmart2000].tail(2)
Out[21]:
storenum OPENDATE date_super conversion st county STREETADDR STRCITY STRSTATE ZIPCODE type_store LAT LON MONTH DAY YEAR
2444 2938 9/20/00 9/20/00 1.0 22 55 2428 WEST PINHOOK ROAD Lafayette LA 70508 Supercenter 30.163368 -92.01974 9 20 2000
2445 2603 1/26/00 NaN NaN 42 3 300 WAL-MART DRIVE Gibsonia PA 15044 Wal-Mart 40.631011 -79.95766 1 26 2000
In [22]:
df = walmart[walmart2000]
fig = px.scatter_geo(df, lat='LAT', lon='LON',
                     color="type_store", # which column to use to set the color of markers
                     hover_name="STRCITY",
                    scope='usa',
                    color_discrete_sequence=['darkblue', 'lightblue'])
fig.show()

6. Load wimbledons_champions.csv and use Google Maps Services Python client to create the following map.

In [23]:
champions = pd.read_csv('wimbledons_champions.csv')
champions
Out[23]:
Gender Champion Mins Runner-up Nationality Champion Nationality Runner-up Score Runner-up Seed Champion Seed Year Runner-up Nationality (Men's) Runner-Up
0 Men's G.L. Patterson NaN AUS AUS N.E. Brookes 6-3, 7-5, 6-2 NaN NaN 1919 NaN NaN
1 Men's G.L. Patterson NaN GBR AUS R. Lycett 6-3, 6-4, 6-2 NaN NaN 1922 NaN NaN
2 Men's N.E. Brookes NaN GBR AUS A.W. Gore 6-4, 6-2, 6-2 NaN NaN 1907 NaN NaN
3 Men's N.E. Brookes NaN NZL AUS A.F. Wilding 6-4, 6-4, 7-5 NaN NaN 1914 NaN NaN
4 Men's J.R. Borotra 80.0 FRA FRA J.R. Lacoste 6-1, 3-6, 6-1, 3-6, 6-4 NaN NaN 1924 NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ...
248 Women's V.E.S. Williams 84.0 NaN USA NaN 6-3, 7-6 (7-3) 2 5.0 2000 USA L.A. Davenport
249 Women's S.J. Fry 50.0 NaN USA NaN 6-3, 6-1, 6 5.0 1956 GBR A. Buxton
250 Women's S. Williams 122.0 NaN USA NaN 6-1, 5-7, 6-2 3 6.0 2012 POL A. Radwańska
251 Women's V.E.S. Williams 111.0 NaN USA NaN 7-5, 6-4 6 7.0 2008 USA S.J. Williams
252 Women's J.R. Susman 57.0 NaN USA NaN 6-4, 6-4, NaN 8.0 1962 TCH C. Sukova

253 rows × 12 columns

a. (Figure 5) Create a map showing the number of champions for different countries.

b. The map should show the entire world. c. Calculate how many Wimbledon champions each country has produced. d. Place a marker for each country that has produced a champion. Use the latitude and longitude of the capital of the country as the location. i. You will need to find the latitude and longitude for the capitals yourself. e. The size of the marker should be proportional to the number of champions this country has produced.

In [24]:
champions['Champion Nationality'].unique()
Out[24]:
array(['AUS', 'FRA', 'GBR', 'NZL', 'USA', 'SRB', 'SUI', 'SWE', 'CZE',
       'ESP', 'GER', 'NED', 'CRO', 'BRA', 'RUS'], dtype=object)
In [25]:
RUS = champions['Champion Nationality'] == 'RUS'
RUS = champions[RUS]
RUS['lat'] = 55.7558
RUS['lon'] = 37.6173
RUS['numWin'] = RUS.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [26]:
BRA = champions['Champion Nationality'] == 'BRA'
BRA = champions[BRA]
BRA['lat'] = -15.8267
BRA['lon'] = -47.9218
BRA['numWin'] = BRA.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [27]:
CRO = champions['Champion Nationality'] == 'CRO'
CRO = champions[CRO]
CRO['lat'] = 45.8150
CRO['lon'] = 15.9819
CRO['numWin'] = CRO.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [28]:
NED = champions['Champion Nationality'] == 'NED'
NED = champions[NED]
NED['lat'] = 52.3667
NED['lon'] = 4.8945
NED['numWin'] = NED.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [29]:
GER = champions['Champion Nationality'] == 'GER'
GER = champions[GER]
GER['lat'] = 52.5200
GER['lon'] = 13.4050
GER['numWin'] = GER.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [30]:
ESP = champions['Champion Nationality'] == 'ESP'
ESP = champions[ESP]
ESP['lat'] = 40.4168
ESP['lon'] = -3.7038
ESP['numWin'] = ESP.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [31]:
CZE = champions['Champion Nationality'] == 'CZE'
CZE = champions[CZE]
CZE['lat'] = 50.0755
CZE['lon'] = 14.4378
CZE['numWin'] = CZE.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [32]:
SWE = champions['Champion Nationality'] == 'SWE'
SWE = champions[SWE]
SWE['lat'] = 59.3293
SWE['lon'] = 18.0686
SWE['numWin'] = SWE.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [33]:
SUI = champions['Champion Nationality'] == 'SUI'
SUI = champions[SUI]
SUI['lat'] = 46.204391
SUI['lon'] = 6.143158
SUI['numWin'] = SUI.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [34]:
SRB = champions['Champion Nationality'] == 'SRB'
SRB = champions[SRB]
SRB['lat'] = 44.7866
SRB['lon'] = 20.4489
SRB['numWin'] = SRB.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [35]:
USA = champions['Champion Nationality'] == 'USA'
USA = champions[USA]
USA['lat'] = 38.9072
USA['lon'] = -77.0369
USA['numWin'] = USA.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [36]:
NZL = champions['Champion Nationality'] == 'NZL'
NZL = champions[NZL]
NZL['lat'] = -41.2865
NZL['lon'] = 174.7762
NZL['numWin'] = NZL.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [37]:
GBR = champions['Champion Nationality'] == 'GBR'
GBR = champions[GBR]
GBR['lat'] = 51.5074
GBR['lon'] = -0.1278
GBR['numWin'] = GBR.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [38]:
AUS = champions['Champion Nationality'] == 'AUS'
AUS = champions[AUS]
AUS['lat'] = -35.2809
AUS['lon'] = 149.1300
AUS['numWin'] = AUS.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [39]:
FRA = champions['Champion Nationality'] == 'FRA'
FRA = champions[FRA]
FRA['lat'] = 48.8566
FRA['lon'] = 2.3522
FRA['numWin'] = FRA.shape[0]
C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:4: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

C:\Users\Juney\Anaconda3\lib\site-packages\ipykernel_launcher.py:5: SettingWithCopyWarning:


A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

In [40]:
champions = AUS.merge(FRA, how='outer')
champions = champions.merge(GBR, how='outer')
champions = champions.merge(NZL, how='outer')
champions = champions.merge(USA, how='outer')
champions = champions.merge(SRB, how='outer')
champions = champions.merge(SUI, how='outer')
champions = champions.merge(SWE, how='outer')
champions = champions.merge(CZE, how='outer')
champions = champions.merge(ESP, how='outer')
champions = champions.merge(GER, how='outer')
champions = champions.merge(NED, how='outer')
champions = champions.merge(CRO, how='outer')
champions = champions.merge(BRA, how='outer')
champions = champions.merge(RUS, how='outer')
In [41]:
champions
Out[41]:
Gender Champion Mins Runner-up Nationality Champion Nationality Runner-up Score Runner-up Seed Champion Seed Year Runner-up Nationality (Men's) Runner-Up lat lon numWin
0 Men's G.L. Patterson NaN AUS AUS N.E. Brookes 6-3, 7-5, 6-2 NaN NaN 1919 NaN NaN -35.2809 149.1300 26
1 Men's G.L. Patterson NaN GBR AUS R. Lycett 6-3, 6-4, 6-2 NaN NaN 1922 NaN NaN -35.2809 149.1300 26
2 Men's N.E. Brookes NaN GBR AUS A.W. Gore 6-4, 6-2, 6-2 NaN NaN 1907 NaN NaN -35.2809 149.1300 26
3 Men's N.E. Brookes NaN NZL AUS A.F. Wilding 6-4, 6-4, 7-5 NaN NaN 1914 NaN NaN -35.2809 149.1300 26
4 Men's R.G. Laver 60.0 AUS AUS A.D. Roche 6-3, 6-4, 6-2 15 1.0 1968 NaN NaN -35.2809 149.1300 26
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
248 Men's G.S. Ivanisevic 182.0 AUS CRO P.M. Rafter 6-3, 3-6, 6-3, 2-6, 9-7 3 NaN 2001 NaN NaN 45.8150 15.9819 1
249 Women's M.E.A. Bueno 59.0 NaN BRA NaN 8-6, 6-0 8 1.0 1960 RSA S. Reynolds -15.8267 -47.9218 3
250 Women's M.E.A. Bueno 90.0 NaN BRA NaN 6-4, 7-9, 6-3, 1 2.0 1964 AUS M. Smith -15.8267 -47.9218 3
251 Women's M.E.A. Bueno 43.0 NaN BRA NaN 6-4, 6-3, 4 6.0 1959 USA D.R. Hard -15.8267 -47.9218 3
252 Women's M. Sharapova 74.0 NaN RUS NaN 6-1, 6-4 1 13.0 2004 USA S.J. Williams 55.7558 37.6173 1

253 rows × 15 columns

In [42]:
champMid = champions['numWin']>=20
champions[champMid]['Champion Nationality'].unique()

champTiny = champions['numWin']<=5
champions[champTiny]['Champion Nationality'].unique()

champSmall = champions['numWin']<=20
champions[champSmall]['Champion Nationality'].unique()
Out[42]:
array(['FRA', 'NZL', 'SRB', 'SUI', 'SWE', 'CZE', 'ESP', 'GER', 'NED',
       'CRO', 'BRA', 'RUS'], dtype=object)
In [43]:
import googlemaps
import io
from IPython.display import Image, display
from googlemaps.maps import StaticMapMarker

apiKey = 'AIzaSyDhkyLC5StbqUSjYFaOV7ZKYzA4t3Q5xcM'
apiKeyPro = 'AIzaSyAbyii-sn8o_L5MsNR1YB7tpgUtb2BvXdk'

gmaps = googlemaps.Client(key=apiKeyPro)

SRBlat = 44.7866
SRBlon = 20.4489

SUIlat = 46.204391
SUIlon = 6.143158

SWElat = 59.3293
SWElon = 18.0686

CZElat = 50.0755
CZElon = 14.4378

ESPlat = 40.4168
ESPlon = -3.7038

GERlat = 52.5200
GERlon = 13.4050

NEDlat = 52.3667
NEDlon = 4.8945

CROlat = 45.8150
CROlon = 15.9819

BRAlat = -15.8267
BRAlon = -47.9218

RUSlat = 55.7558
RUSlon = 37.6173

my_markers = [StaticMapMarker(
    locations={'lat':-35.2809, 'lng':149.1300},
    size="mid", color="blue", label='A'),
             StaticMapMarker(
    locations={'lat':48.8566, 'lng':2.3522},
    size="small", color="blue", label='F'),
             StaticMapMarker(
    locations={'lat':51.5074, 'lng':-0.1278},
    size="mid", color="blue", label='G'),
             StaticMapMarker(
    locations={'lat':-41.2865, 'lng':174.7762},
    size="tiny", color="blue", label='N'),
             StaticMapMarker(
    locations={'lat':38.9072, 'lng':-77.0369},
    size="mid", color="blue", label='U'),
             StaticMapMarker(
    locations={'lat':44.7866, 'lng':20.4489},
    size="tiny", color="blue", label='S'),
             StaticMapMarker(
    locations={'lat':SUIlat, 'lng':SUIlon},
    size="small", color="blue", label='S'),
             StaticMapMarker(
    locations={'lat':SWElat, 'lng':SWElon},
    size="small", color="blue", label='S'),
             StaticMapMarker(
    locations={'lat':CZElat, 'lng':CZElon},
    size="tiny", color="blue", label='C'),
             StaticMapMarker(
    locations={'lat':ESPlat, 'lng':ESPlon},
    size="tiny", color="blue", label='E'),
             StaticMapMarker(
    locations={'lat':GERlat, 'lng':GERlon},
    size="small", color="blue", label='G'),
             StaticMapMarker(
    locations={'lat':NEDlat, 'lng':NEDlon},
    size="tiny", color="blue", label='N'),
             StaticMapMarker(
    locations={'lat':CROlat, 'lng':CROlon},
    size="tiny", color="blue", label='C'),
             StaticMapMarker(
    locations={'lat':BRAlat, 'lng':BRAlon},
    size="tiny", color="blue", label='B'),
             StaticMapMarker(
    locations={'lat':RUSlat, 'lng':RUSlon},
    size="tiny", color="blue", label='R')]

my_file_name = "goolgmaps.png"
f = open(my_file_name, 'wb')

for chunk in gmaps.static_map(size=(4000, 4000), markers = my_markers):
    if chunk:
        f.write(chunk)
f.close()

display(Image(filename=my_file_name))
In [ ]: